/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.anny.order.service;

import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.anny.order.entity.AnnyOrder;
import com.jeeplus.modules.anny.order.mapper.AnnyOrderMapper;
import com.jeeplus.modules.anny.order.entity.AnnyOrderGoods;
import com.jeeplus.modules.anny.order.mapper.AnnyOrderGoodsMapper;

/**
 * 订单管理Service
 * @author clf
 * @version 2018-10-18
 */
@Service
@Transactional(readOnly = true)
public class AnnyOrderService extends CrudService<AnnyOrderMapper, AnnyOrder> {

	@Autowired
	private AnnyOrderGoodsMapper annyOrderGoodsMapper;

    @Autowired
    private AnnyOrderMapper annyOrderMapper;

	public AnnyOrder get(String id) {
		AnnyOrder annyOrder = super.get(id);
		annyOrder.setAnnyOrderGoodsList(annyOrderGoodsMapper.findList(new AnnyOrderGoods(id)));
		return annyOrder;
	}
	
	public List<AnnyOrder> findList(AnnyOrder annyOrder) {
		return super.findList(annyOrder);
	}
	
	public Page<AnnyOrder> findPage(Page<AnnyOrder> page, AnnyOrder annyOrder) {
		return super.findPage(page, annyOrder);
	}
	
	@Transactional(readOnly = false)
	public void save(AnnyOrder annyOrder) {
		super.save(annyOrder);
		for (AnnyOrderGoods annyOrderGoods : annyOrder.getAnnyOrderGoodsList()){
			if (annyOrderGoods.getId() == null){
				continue;
			}
			if (AnnyOrderGoods.DEL_FLAG_NORMAL.equals(annyOrderGoods.getDelFlag())){
				if (StringUtils.isBlank(annyOrderGoods.getId())){
					annyOrderGoods.preInsert();
					annyOrderGoodsMapper.insert(annyOrderGoods);
				}else{
					annyOrderGoods.preUpdate();
					annyOrderGoodsMapper.update(annyOrderGoods);
				}
			}else{
				annyOrderGoodsMapper.delete(annyOrderGoods);
			}
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(AnnyOrder annyOrder) {
		super.delete(annyOrder);
//		annyOrderGoodsMapper.delete(new AnnyOrderGoods(annyOrder));
	}


	public List<AnnyOrder> Pendingpay(String userid) {
        return annyOrderMapper.pendingpay(userid);
    }

    public List<AnnyOrder> Pendingdelivery(String userid) {
        return annyOrderMapper.pendingpay(userid);
    }


	public List<AnnyOrder> Goodstobereceived(String userid) {
		return annyOrderMapper.Goodstobereceived(userid);
	}

	public List<AnnyOrder> Completed(String userid) {
		return annyOrderMapper.Completed(userid);
	}


}